<template>
  <el-dialog
    @close="closeDialog"
    v-model="dialogVisible"
    :title="title"
    width="400px"
  >
    <el-form
      ref="ruleFormRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="formData.typeName" maxlength="20" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="formData.state" style="width: 100%">
          <el-option
            :value="item.value"
            :label="item.label"
            v-for="item in stateList"
            :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字典排序" prop="sort">
        <el-input-number v-model="formData.sort" :min="0" style="width: 100%" />
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button
        @click="submitForm(ruleFormRef)"
        type="primary"
        :loading="submitLoading"
      >
        保存
      </el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed, reactive, ref, watch } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { ApiResult } from "@/util/request";
import { saveOrUpdateVideoType } from "@/api/videoManage.ts";

interface IForm {
  id: string;
  parentId: string;
  sort: string;
  state: string;
  typeName: string;
}
const stateList = [
  { value: "1", label: "启用" },
  { value: "2", label: "禁用" },
];
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Object,
    default: () => {},
  },
  parentMenuOption: {
    type: Array,
    default: () => [],
  },
});

const emit = defineEmits(["update:visible", "update:data", "submit"]);

const ruleFormRef = ref();
const submitLoading = ref<boolean>(false);

const formData = reactive<IForm>({
  id: "",
  parentId: "0",
  sort: "1",
  state: "",
  typeName: "",
});

//TODO 做成动态
const rules = reactive<FormRules<IForm>>({
  typeName: [{ required: true, message: "请输入", trigger: "blur" }],
});

const dialogVisible = computed({
  get() {
    return props.visible;
  },

  set(visible: boolean) {
    emit("update:visible", visible);
  },
});

//TODO 做成动态
const title = computed(() => {
  return formData?.id ? "编辑" : "新增";
});

function closeDialog() {
  resetForm(ruleFormRef.value);
  emit("update:visible", false);
  emit("update:data", null);
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    let result: ApiResult<any>;
    if (valid) {
      submitLoading.value = true;
      formData.parentId = "0";
      result = await saveOrUpdateVideoType(formData);
      const { success, msg } = result;
      submitLoading.value = false;
      if (!success) {
        ElMessage.error(msg);
        return;
      }
      ElMessage.success("操作成功");
      closeDialog();
      emit("submit");
    } else {
      console.log("error submit!", fields);
    }
  });
};
watch(dialogVisible, (visible) => {
  Object.keys(formData).forEach((key: any) => {
    formData[key as keyof IForm] = visible ? props.data?.[key] || "" : "";
  });
  formData.sort = props.data?.sort ? props.data.sort : "1";
  formData.state = formData.state.toString();
});
</script>

<style scoped lang="scss">
.footer {
  width: 100%;
  text-align: center;
  margin-top: 50px;
}
.upload-demo {
  width: 100%;
}
</style>
